﻿' Copyright (c) Microsoft Corporation.  All rights reserved.

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' take care of the post back check
        If IsPostBack Then
            Return
        End If

        Using travelEntities As New MyTravelEntities
            ' get the travel blogs.
            Dim blogs = From tb In travelEntities.Blogs _
                        Where tb.BlogPosts.Count > 0 _
                        Select tb

            ' bind to the data source
            TravelBlogsGrid.DataSource = blogs
            TravelBlogsGrid.DataBind()

            ' get the name of the people, and store the list of people in view state.
            dropDownPeople.DataTextField = "Name"
            Dim people = travelEntities.People.ToList()
            ViewState("People") = people

            dropDownPeople.DataSource = people
            dropDownPeople.DataBind()
        End Using
    End Sub

    Protected Sub buttonCreateNewBlog_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonCreateNewBlog.Click
        ' get the people out and get the id to past to the new blog post page.
        Dim people = ViewState("People")
        Dim peopleID = people(dropDownPeople.SelectedIndex).PersonID

        ' create a response and redirect passing the person ID.
        Response.Redirect("NewBlogPost.aspx?PersonID=" & peopleID.ToString())
    End Sub
End Class

